package demo.jsonview;

import act.controller.Controller;
import act.db.morphia.MorphiaAdaptiveRecord;
import act.db.morphia.MorphiaDao;
import org.mongodb.morphia.annotations.Entity;
import org.osgl.mvc.annotation.DeleteAction;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.mvc.annotation.PutAction;

import java.util.Map;

import static act.controller.Controller.Util.notFoundIfNull;

@Entity("article")
public class Article extends MorphiaAdaptiveRecord<Article> {

    @Controller("article")
    public static class Service extends MorphiaDao<Article> {

        @GetAction
        public Iterable<Article> list() {
            return findAll();
        }

        @GetAction("{id}")
        public Article show(String id) {
            return findById(id);
        }

        @PostAction
        public Article create(Article article) {
            return save(article);
        }

        @PutAction("{id}")
        public Article update(String id, Map<String, Object> data) {
            Article article = findById(id);
            notFoundIfNull(article);
            return save(article.mergeValues(data));
        }

        @DeleteAction("id")
        public void delete(String id) {
            deleteById(id);
        }

    }

}
